package com.atguigu.principle.ocp.improve;

/**
 * @author ccc
 * @create 2020-07-10 18:47
 */
public class Ocp {
    public static void main(String[] args) {
        GraphicEditor graphicEditor = new GraphicEditor();
        graphicEditor.drawShape(new Triangle());
        graphicEditor.drawShape(new Circle());
        graphicEditor.drawShape(new Rectangle());
        graphicEditor.drawShape(new OtherShape());
    }
}

//画图的类【使用方】
class GraphicEditor {
    //接收Share对象，然后根据type绘制不同的图形
    public void drawShape(Shape s) {
        s.draw();
    }
}

//Shape类 基类
abstract class Shape {
    int m_type;
    //加入抽象方法draw
    public abstract void draw();
}

//矩形
class Rectangle extends Shape {
    @Override
    public void draw() {
        System.out.println("绘制矩形 ");
    }

    Rectangle() {
        super.m_type = 1;
    }
}

//圆
class Circle extends Shape {
    Circle() {
        super.m_type = 2;
    }

    @Override
    public void draw() {
        System.out.println("绘制圆形");
    }
}

class Triangle extends Shape {
    public Triangle() {
        super.m_type = 3;
    }

    @Override
    public void draw() {
        System.out.println("绘制三角形");
    }
}

//绘制其它图形
class OtherShape extends Shape{
    public OtherShape() {
        super.m_type = 4;
    }

    @Override
    public void draw() {
        System.out.println("绘制其它图形");
    }
}